package com.dmcloud.common.util.tools;

import org.apache.commons.lang3.time.DateUtils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Created by xiaodao
 * date: 2020/5/18
 */
public class DateUtil extends DateUtils {
    public static Date LocalDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        java.util.Date date = Date.from(instant);
        return date;
    }
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }
    public static LocalDateTime DateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime;
    }

    /**
     * jdk8  date  to  string
     * @param date
     * @param pattern
     * @return
     */
    public static String LocalDateTimeFormatToString(Date date,String  pattern){
        DateTimeFormatter dateTimeFormatter =  DateTimeFormatter.ofPattern("yyyy-mm-dd HH:mm");
        LocalDateTime localDateTime = DateUtil.DateToLocalDateTime(date);
        String format = localDateTime.format(dateTimeFormatter);
        return format;

    }

    /**
     * 当前日期
     * @return
     */
    public static Date now() {
        return new Date();
    }

}
